#include "init.h"
#include "ethernet.h"
#include "stdio.h"
#include "unistd.h"
#include "msgList.h"
#include "data_proc.h"

using namespace std;


int main(int argc, char * argv[])
{
    class CConfig * globalConfig = CConfig::getGlobleConfig();

    connection_config * pstConnConfig = globalConfig->getConnConfig();

    CRecvPool * pRecvPool = new CRecvPool();
	flagDataProcRunning = 1;
	int retCreate = pthread_create(&dataProc_ThreadHandle,NULL,threadFunc_dataProc,pRecvPool);
	if(retCreate == 0)
	{
        cout<<"start data proc thread success\n"<<endl;
	}
	else
	{
		cout<<"failed to start data proc thread,error:\n"<<retCreate;
	}
	g_ptrRecvPool = pRecvPool;

    CEthernet * pEthLocal = new CEthernet();
	int startRet = startLocalUdp(pEthLocal);
	
    class CEthernet * pEthernet = new CEthernet();
    pEthernet->setConnectionConfig(pstConnConfig);
    startRet = pEthernet->startConnection(0,PROTOCOL_TCP);

    
    
    if(startRet == 0)
    {
       printf("start connection success\n");
       sleep(1);

    }
    else
    {
    	printf("start connection failed,error code is[%d]\n",startRet);
    }
    //char ab = '\0';
    while(1)//(ab != 'q')
    {
    	sleep(1);
    	/*fd_set writefds;
		fd_set readfds;
		fd_set exceptfds;
		FD_ZERO(&readfds);
		FD_ZERO(&exceptfds);
		FD_ZERO(&writefds);
		FD_SET(0, &readfds);
		struct timeval timeoutVal;
		timeoutVal.tv_usec = 500;
		timeoutVal.tv_sec = 0;
		int selRet = select(
			1,
			&readfds,
			&writefds,
			&exceptfds,
			&timeoutVal
			);
		if(selRet == 1)
		{
            int getRet = getchar();
            ab = (char)getRet;
            printf("char get is %c\n",(char)getRet);
		}*/
    }

    pEthernet->recvThreadRunningFlag = STOPPING;
    pEthernet->joinRecvThread();

    printf("thread exit info %d\n",pEthernet->recvThreadExitVal);
    //printf("exit the progress now\n");
    delete pEthernet;
	delete pEthLocal;
    delete globalConfig;
	flagDataProcRunning = 0;
	int * retJoin = NULL;
	pthread_join(dataProc_ThreadHandle,(void**)&retJoin);
	delete pRecvPool;
	g_ptrRecvPool = NULL;
	pRecvPool = NULL;
    return 0;
}
